#include "stdafx.h"
#include "time_settings.h"

TimeSettings::TimeSettings(Common* _common)
	: common(_common)
{
	init_model();
}


TimeSettings::~TimeSettings(){}

void TimeSettings::init_model(){
	//0 - combobox/boolean
	//1 - spinbox
	//2 - textbox
	//3 - label
	QString common_parameter = "COMMON";
	vector<QString> list_of_script = common->getScriptList();
	model = new ParametersModel(this);
	model->addItem(common_parameter, "SIM SPEED", 1, "", 1, 1,"simulation speed");
	model->addItem(common_parameter, "DT", 1, "s", 1, 1,"2222222");
	foreach(QString script_name, list_of_script)
		addScriptParameter(script_name);
	delegate = new Delegate(common->getParametersWidget());
	connect(model, SIGNAL(ItemChanged(const QString &, const QVariant &)),
		this, SLOT(getNewParameterValue(const QString &, const QVariant &)));
	connect(this, SIGNAL(setNewParameterValue(const QString &, const QVariant &)), 
		model, SLOT(changeData(const QString &, const QVariant &)));
	connect(common, SIGNAL(scriptAdded(const QString&)), this, SLOT(addScriptParameter(const QString&)));
	connect(common, SIGNAL(scriptRemoved(const QString&)), this, SLOT(removeScriptParameter(const QString&)));
}

void TimeSettings::addScriptParameter(const QString &script_name){
	model->addItem(script_name, "END TIME", 0, "s", 1, 1,"END TIME");
}

void TimeSettings::removeScriptParameter(const QString &script_name){
	model->removeItems(script_name);
}

void TimeSettings::update_parameters(){
	common->getParametersWidget()->setItemDelegate(delegate);
}

void TimeSettings::getNewParameterValue(const QString &_name, const QVariant &_value){
	if (_name == "DT"){
		if (_value.toDouble() < 0.1)
			setNewParameterValue(_name, 0.1);
	}
}